<?php
//session_start(); 
//include "application.php";
//include "common.php";
//application_start(); 

//dim $strChatLine,$strStyle;
 

$strChatLine = "[" . date("n/j/Y g:i:s A") . "]&nbsp;<B>" . $strChatName . ":</B> " . $strChatInput; 
 
if (strlen($strLogin) > 0) {
	
	$pattern = "/\|" . $strChatName . "/i";
	//if (strpos($strUsers, App($strChatName)) < 1 ) 
	if (!preg_match($pattern, $strUsers))	
	{
		$strChatBody = $strChatBody . '<BR><FONT COLOR="#BB0000">[' . date("n/j/Y g:i:s A") . "]&nbsp;<B>" . $strChatName . " " . $strChatInput . "</B></FONT>" . "\n";
		//$strUsers = str_replace(App($strChatName),"",$strUsers);
		$strUsers = $strUsers . App($strChatName);		
		//$strUsers = ($strUsers != "")? $strUsers . App($strChatName) : $strChatName;
		//Response.$Cookies["ChatName"] = trim($strChatName);
		//Response.$Cookies["ChatName"].$Expires = Date + 365;
 
		//Application("Timeout_".$strChatName) = DateAdd("n", $intChatTimeOut, date("n/j/Y g:i:s A")() );	
		$_APP["Timeout_".$strChatName] = date("n/j/Y g:i:s A"); 

	} else {
		//$strUserConflict = "true";
		
	}

	
} elseif (strtolower(@$_POST["chatSubmit"]) == "log out")
   {
	//if (strlen(trim($strChatName)) == 0) {$strChatName = Request.$Cookies["ChatName"];}
	$strChatBody = $strChatBody . '<BR><FONT COLOR="#000000">[' . date("n/j/Y g:i:s A") . "]&nbsp;<B>" . $strChatName . " has left the room</B></FONT>" . "\n";
	$strUsers = str_replace(App($strChatName),"",$strUsers);
	$strLogout = "true";
	//$strChatBody="";
   
} else {
 
	//Application("Timeout_".$strChatName) = DateAdd("n", $intChatTimeOut, date("n/j/Y g:i:s A")() );
 	$_APP["Timeout_".$strChatName] = date("n/j/Y g:i:s A");
 	
//	Check for commands
	if (substr($strChatInput,1) == "/") {
		switch (True){
			Case strpos(strtolower($strChatInput),"clear") > 0 :
				$strChatBody = "";
				$strChatLine = "<B>".$strChatName." wiped the screen</B>";
				break;
				
			Case strpos(strtolower($strChatInput),"/nick") == 1 :
				$strNewChatName = trim(str_replace("/nick","",$strChatInput));
 
				if (strpos($strUsers, App($strNewChatName)) < 1) 
				{
					if (strlen(trim($strNewChatName)) > 0) 
					{
						$strChatLine = "[" . date("n/j/Y g:i:s A") . "]&nbsp;<B>" . $strChatName . " is now known as " . $strNewChatName . "</B>";
						$strUsers = str_replace( App($strChatName), App($strNewChatName),$strUsers);
						$strChatName = $strNewChatName;
						Response.$Cookies["ChatName"] = trim($strChatName);
					} else {
						$strChatLine = "[" . date("n/j/Y g:i:s A") . "]&nbsp;<B>" . $strChatName . " is screwing with the application</B>";
					}
					$strNewNick = "true";
				} else {
					$strUserConflict = "true";
				}
				break;
				
			Case strpos(strtolower($strChatInput),"/me") == 1 : 
				$strChatLine = str_replace("/me",$strChatName,$strChatInput);
				break;
				
			Case (strpos(strtolower($strChatInput),"/logout") == 1 || strpos(strtolower($strChatInput),"/lo") == 1) :
				$strChatBody = $strChatBody . '<BR><FONT COLOR="#000000">[' . date("n/j/Y g:i:s A") . "]&nbsp;<B>" . $strChatName . " has left the room</B></FONT>" . "\n";
				$strUsers = str_replace($App[$strChatName],"",$strUsers);
				$strLogout = "true";
				$strChatBody="";
				break;
				
		}
		
	}
//	Check for stylistic preferences
	if ($strFont != "Default" && strlen($strFont) != 0) {
		$strStyle = "font-family:" . $strFont . ";" ;
		Response.$Cookies["Font"] = $strFont;
	}
	if ($strSize != "Default" && strlen($strSize) != 0) {
		$strStyle = $strStyle . "font-size:" . $strSize . "pt;";
		Response.$Cookies["Size"] = $strSize;
	}
	if ($strColor != "Default" && strlen($strColor) != 0) {
		$strStyle = $strStyle . "color:" . $strColor . ";";
		Response.$Cookies["Style"] = $strStyle;
	}
//	Aaand slap it all together.
	if ($strLogout != "true" && $strUserConflict != "true") {
		$strChatLine = '<BR><A STYLE="' . $strStyle . '">' . $strChatLine . "</A>";
		$strChatBody = $strChatBody.$strChatLine."\n";
	}
}
 
function App($strUserName) //$App[$strUserName] 
{
	return  "|" . $strUserName;
}
?>
